package com.flyco.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.pms.entity.PmsProductPriceRegulate;
import com.flyco.modules.pms.mapper.PmsProductPriceRegulateMapper;
import com.flyco.modules.pms.model.PmsProductSkuModel;
import com.flyco.modules.pms.service.IPmsProductPriceRegulateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 商品价格管控
 * @Author: flyco
 * @Date:   2020-02-26
 * @Version: V1.0
 */
@Service
public class PmsProductPriceRegulateServiceImpl extends ServiceImpl<PmsProductPriceRegulateMapper, PmsProductPriceRegulate> implements IPmsProductPriceRegulateService {

    @Autowired
    private PmsProductPriceRegulateMapper productPriceRegulateMapper;

    @Override
    public PmsProductPriceRegulate getByProductSn(String productSn) {
        LambdaQueryWrapper<PmsProductPriceRegulate> regulateQueryWrapper = new LambdaQueryWrapper<>();
        regulateQueryWrapper.eq(PmsProductPriceRegulate::getProductSn,productSn);
        return this.getOne(regulateQueryWrapper);
    }

    @Override
    public List<PmsProductSkuModel> getProductSkuModel(Long partnerId){
        return productPriceRegulateMapper.getProductSkuModel(partnerId);
    }


    @Override
    @Transactional
    public void updateByListWithOutId(List<PmsProductPriceRegulate> regulates, Long partnerId){
        LambdaQueryWrapper<PmsProductPriceRegulate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductPriceRegulate::getPartnerId, partnerId);
        List<PmsProductPriceRegulate> regulateList = this.list(wrapper);
        List<String> dbProductSnList = regulateList.stream().map(e -> e.getSkuCode()).collect(Collectors.toList());
        Map<String, PmsProductPriceRegulate> dbMap = regulateList.stream().collect(Collectors.toMap(PmsProductPriceRegulate::getSkuCode, Function.identity(), (key1, key2) -> key2));

        List<String> syncProductSnList = regulates.stream().map(e -> e.getSkuCode()).collect(Collectors.toList());

        dbProductSnList.removeAll(syncProductSnList);//数据库中存在的，同步过来的时候没有的，需要删除
        if ( ! CollectionUtils.isEmpty(dbProductSnList)) {
            delByProductSns(dbProductSnList, partnerId);
        }

        List<PmsProductPriceRegulate> addList = new ArrayList<>();
        List<PmsProductPriceRegulate> updateList = new ArrayList<>();

        for ( PmsProductPriceRegulate regulate : regulates ) {
            PmsProductPriceRegulate dbRegulate = dbMap.get(regulate.getSkuCode());
            if (oConvertUtils.isEmpty(dbRegulate)){
                addList.add(regulate);
            } else {
                regulate.setId(dbRegulate.getId());
                regulate.setSyncTime(new Date());
                updateList.add(regulate);
            }
        }

        if ( ! CollectionUtils.isEmpty(addList)) {
            this.saveBatch(addList);
        }

        if ( ! CollectionUtils.isEmpty(updateList)) {
            this.updateBatchById(updateList);
        }

        /*for (PmsProductPriceRegulate regulate : regulates) {
            LambdaQueryWrapper<PmsProductPriceRegulate> regulateQueryWrapper = new LambdaQueryWrapper<>();
            regulateQueryWrapper.eq(PmsProductPriceRegulate::getProductSn,regulate.getProductSn());
            this.count(regulateQueryWrapper);
        }*/
    }


    private void delByProductSns(List<String> list, Long partnerId) {
        LambdaQueryWrapper<PmsProductPriceRegulate> regulateQueryWrapper = new LambdaQueryWrapper<>();
        regulateQueryWrapper.in(PmsProductPriceRegulate::getSkuCode,list);
        regulateQueryWrapper.eq(PmsProductPriceRegulate::getPartnerId, partnerId);
        this.remove(regulateQueryWrapper);
    }
}
